/**
 * 
 *  @project Air Traff Monitor
 * 
 *  @author M.Emre ATASEVER <m.emre.atas@gmail.com>
 *  @author M.Burak ALKAN <mburakalkan@gmail.com>
 *  @author H.Burak KARAMIK <bkaramik@gmail.com>
 *
 *	`Air Traffic Monitor' is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "alertmodel.h"
#include <QtAlgorithms>

AlertModel::AlertModel(QKismetSocket *sock,QObject *parent) : KismetDataModel<AlertRootItem,AlertItem>(sock)
{
	connect(socket,SIGNAL(alInfo(alertInfo)),this,SLOT(alInfo(alertInfo)));
	//TODO: Setup signals etc
}

QVariant AlertModel::data(const QModelIndex &index, int role) const
{
	if(!index.isValid())
		return QVariant();
	if(role != Qt::DisplayRole)
		return QVariant();

	AlertItem *item	= static_cast<AlertItem*>(index.internalPointer());
	return item->data(index.column());
}

QVariant AlertModel::headerData(int section, Qt::Orientation orientation,int role) const
{
	if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
		return socket->alert.getNames()->at(section);
	return QVariant();
}

QModelIndex AlertModel::index(int row, int column, const QModelIndex &parent) const
{
	if(!hasIndex(row,column,parent))
		return QModelIndex();
	
	
	AlertItem *it	= rootItem->items[row];
	if(it)
		return createIndex(row,column,it);
	else
		return QModelIndex();
}

QModelIndex AlertModel::parent(const QModelIndex &index) const
{
	if(index.isValid())
		return QModelIndex();
	
	return QModelIndex();
}

int AlertModel::columnCount(const QModelIndex&) const
{
	return socket->alert.getNames()->size();
}

void AlertModel::alInfo(alertInfo info)
{
	AlertItem *item	= findItem(info.props[0].toInt());
	int c	= rootItem->items.size();
	
	if(item)
	{
		//Update existing item
		int s	= info.idx->size();
		if(s > 1)
		{
			item->setData(&info);
			int r 	= item->row();
			emit dataChanged(createIndex(r,info.idx->at(1),item),createIndex(r,info.idx->at(s-1),item));
		}
	}
	else
	{
		beginInsertRows(QModelIndex(),c,c);
	
		item	= new AlertItem(&info,rootItem);
		rootItem->items.append(item);
	
		endInsertRows();
	}
}

AlertItem* AlertModel::findItem(int time)
{
	QDateTime tm	= QDateTime::fromTime_t(time);

	int j	= rootItem->items.size();
	for(int i = 0;i < j;i++)
		if(rootItem->items[i]->time == tm)
			return rootItem->items[i];
	return 0;
}
//AlertItem implementation

AlertItem::AlertItem(alertInfo *data, AlertRootItem *parent)
{
	root	= parent;
	setData(data);

	time	= QDateTime::fromTime_t(data->props[0].toInt());
}

AlertItem::~AlertItem()
{
	//TODO: clean clients
}

QVariant AlertItem::data(int column)
{
	if(column == 0)
		return time.toString();
	else
	{
		int j	= itemData.size();
		for(int i = 0;i < j;i++)
			if(itemData[i].second == column)
				return AlertOptions::print(&itemData[i].first,column);
	}
	return QVariant();
}

void AlertItem::setData(alertInfo *data)
{
	itemData.clear();
	int j = data->props.size();
	for(int i = 1;i<j;i++)
		itemData.append(qMakePair(data->props[i],data->idx->at(i)));
}

AlertRootItem* AlertItem::parent()
{
	return root;
}

int AlertItem::row()
{
	return root->items.indexOf(this);
}
